// pages/setting.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    initValue: {}, //初始数据
    isSoundOn: true, //输入的数据
    isVibrateOn: true,
    host: '',
    mac: '',
    showSearchDlg: false,
    searchDlgBtns: [{
        type: 'primary',
        className: '',
        text: '搜索WiFi',
        value: 0
      },
      {
        type: 'default',
        className: '',
        text: '搜索蓝牙',
        value: 1
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  onShow: function () {
    this.setData({
      initValue: getApp().globalData,
      isSoundOn: getApp().globalData.isSoundOn,
      isVibrateOn: getApp().globalData.isVibrateOn,
      host: getApp().globalData.host,
      mac: getApp().globalData.mac
    });
  },
  onUnload: function () {
    //console.log("保存表单")
    var formData = this.data.initValue;
    formData.isSoundOn = this.data.isSoundOn;
    formData.isVibrateOn = this.data.isVibrateOn;
    formData.host = this.data.host;
    formData.mac = this.data.mac;
    let that = this;
    wx.setStorage({
      key: 'settings',
      data: formData,
      success:()=>{
        getApp().readSettingsSync();
      },
      fail: () => {
        wx.showToast({
          title: '保存失败',
          icon: 'error'
        })
      }
    });
  },
  changeSound(e) {
    this.setData({
      isSoundOn: e.detail.value
    });
  },
  changeVibrate(e) {
    this.setData({
      isVibrateOn: e.detail.value
    });
  },
  inputIP(e) {
    this.setData({
      host: e.detail.value
    });
  },
  inputMAC(e) {
    this.setData({
      mac: e.detail.value
    });
  },
  doScreenshot() {
    var url = 'http://' + getApp().globalData.host + ":8080/v1/screenshot?t=" + new Date().getTime();
    //console.log("截屏" + url)
    wx.previewImage({
      showmenu:true,
      urls: [url]
    });
  },
  openHelp() {
    if (!this.data.initValue.isRead) {
      var initValue = getApp().globalData;
      initValue.isRead = true;
      this.setData({
        initValue
      });
      wx.setStorage({
        data: initValue,
        key: 'settings',
      });
    };
  },
  onClickDlgBtn(e) {
    this.setData({
      showSearchDlg: false
    });
    if (e.detail.item.value == 0) {
      wx.navigateTo({
        url: '../search/wifi/wifi',
      })
    } else {
      wx.navigateTo({
        url: '../search/bluetooth/bluetooth',
      })
    }
  },
  openSearch() {
    this.setData({
      showSearchDlg: true
    })
  }
})